﻿using Newtonsoft.Json;
using NPOI.SS.Formula.Eval;
using OSSUtility;
using OSSUtility.Device;
using Ozone.Help;
using Rubyer.Converters;
using ServiceStack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Ozone.Device
{
    public class DevR300 : DevBase
    {
        public DevR300(int CommType, string FSrvIP, int FPort) : base(CommType, FSrvIP, FPort)
        {

        }


        /// <summary>
        /// 旋蒸仪真空
        /// </summary>
        /// <param name="nVal"></param>
        /// <returns></returns>
        public double GetVacuum(int nVal)
        {
            try
            {
                string sCommand = "vacuum," + nVal;
                byte[] byteRcv = session_.sendAndReadByte(sCommand);
                if (byteRcv == null)
                    return -1;
                string str = Encoding.UTF8.GetString(byteRcv);
                if (str.Contains("OK"))
                {
                    string strVal = str.Substring(str.LastIndexOf(",") + 1);
                    //bool IsOk = false;
                    //int nT = 0;
                    //while (!IsOk)
                    //{
                    //    if (nT >= 4)
                    //        return false;
                    //    Thread.Sleep(5000);
                    //    byte[] byteRcvOk = session_.sendAndReadByte(sCommand);
                    //    string strOk = Encoding.UTF8.GetString(byteRcvOk);
                    //    string strVal = strOk.Substring(strOk.LastIndexOf(",") + 1);
                    //    if ((double.Parse(strVal) >= (nVal-20)) && (double.Parse(strVal) <= (nVal + 50)))
                    //    {
                    //        IsOk = true;
                    //    }
                    //    nT++;
                    //}
                    return Utility.str2double(strVal);
                }
                return -1;
            }
            catch (Exception ex)
            {
                
                return -1;
            }
        }


        /// <summary>
        /// 旋蒸仪旋转
        /// </summary>
        /// <param name="nVal"></param>
        /// <returns></returns>
        public bool setRotation(int nVal)
        {
            try
            {
                string sCommand = "rotation," + nVal;
                byte[] byteRcv = session_.sendAndReadByte(sCommand);
                if (byteRcv == null)
                    return false;
                string str = Encoding.UTF8.GetString(byteRcv);
                if (str.Contains("OK"))
                {
                    bool IsOk = false;
                    int nT = 0;
                    while (!IsOk)
                    {
                        if (nT >= 20)
                            return false;
                        Thread.Sleep(1000);
                        byte[] byteRcvOk = session_.sendAndReadByte(sCommand);
                        string strOk = Encoding.UTF8.GetString(byteRcvOk);
                        string strVal = strOk.Substring(strOk.LastIndexOf(",") + 1);
                        //旋蒸仪器旋转速度大于0 代表已经开始旋转了
                        if (nVal > 0)
                        {
                            if (double.Parse(strVal) > 0)
                            {
                                IsOk = true;
                            }
                        }

                        if (nVal == 0)
                        {
                            return true;
                        }

                        nT++;
                    }
                    return true;
                }
                return false;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        /// <summary>
        /// 旋蒸仪升降
        /// </summary>
        /// <param name="nVal"></param>
        /// <returns></returns>
        public bool setLift(int nVal)
        {
            try
            {
                string sCommand = "lift," + nVal;
                byte[] byteRcv = session_.sendAndReadByte(sCommand);
                if (byteRcv == null)
                    return false;
                string str = Encoding.UTF8.GetString(byteRcv);
                if (str.Contains("OK"))
                {
                    bool IsOk = false;
                    int nT = 0;
                    while (!IsOk)
                    {
                        if (nT >= 6)
                            return false;
                        Thread.Sleep(3000);
                        byte[] byteRcvOk = session_.sendAndReadByte(sCommand);
                        string strOk = Encoding.UTF8.GetString(byteRcvOk);
                        string strVal = strOk.Substring(strOk.LastIndexOf(",") + 1);
                        if (double.Parse(strVal) == nVal)
                        {
                            IsOk = true;
                        }
                        nT++;
                    }
                    return true;
                }
                return false;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        /// <summary>
        /// 旋蒸仪加热
        /// </summary>
        /// <param name="nVal"></param>
        /// <returns></returns>
        public bool setHeating(int nVal)
        {
            try
            {
                string sCommand = "heating," + nVal;
                byte[] byteRcv = session_.sendAndReadByte(sCommand);
                string str = Encoding.UTF8.GetString(byteRcv);
                if (str.Contains("heating"))
                {
                    return true;
                }
                return false;
            }
            catch (Exception e)
            {
                return false;
            }
        }
    }
}
